<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/* Start of file cash.php */
/* Location: ./cashflow/controllers/cash.php */
class Cash extends CI_Controller {
	
	function __construct(){
		parent::__construct();
		$this->load->module_model('billing-ar','customers'); /** load table customers **/
        $this->load->module_model('billing-ar','invoices'); /** load table invoices **/
        $this->load->model('cashs'); /** load table cash and etc **/
        $this->load->model('budgets'); /** load table Accounts **/
        if(!$this->session->userdata('username')) /** restrict area if not acces to login page**/
			redirect('/',301); 
    }
    
    function payment(){
        $this->ws->set('title',lang('invoice_payment'));/** title page Invoice Payment */
		$this->ws->set('username',$this->session->userdata('username')); /** username session */
		$this->ws->set('department',$this->session->userdata('dep')); /** departement session */
        /** if tax session already or no to view data **/
        if($this->session->userdata('invoiceCash_month_start')):
            $month_start  =   $this->session->userdata('invoiceCash_month_start');
            $year_start   =   $this->session->userdata('invoiceCash_year_start');
            $month_end    =   $this->session->userdata('invoiceCash_month_end');
            $year_end     =   $this->session->userdata('invoiceCash_year_end');
            $customer     =   $this->session->userdata('invoiceCash_customer');
        else:
            $month_start  =   '-';
            $year_start   =   '-';
            $month_end    =   '-';
            $year_end     =   '-';
            $customer     =   '-';
        endif;
        $this->ws->set('month_start',$month_start); /** variabel date month start from session **/
        $this->ws->set('year_start',$year_start); /** variabel date year start from session **/
        $this->ws->set('month_end',$month_end); /** variabel date month end from session **/   
        $this->ws->set('year_end',$year_end); /** variabel date year end from session **/
        $this->ws->set('customer',$customer); /** variabel customer from session **/
        $this->ws->set('customers',$this->customers->getCustomerData('All')); /** Get Data Table Customers */
        $this->ws->set('cash',$this->invoices->getInvoicePayment($month_start,$year_start,$month_end,$year_end,$customer));
        $this->ws->set_theme('smooth'); /** set theme to smooth **/
		$this->ws->view('cash/index_payment'); /** view layout to views/cash/index_payment **/
    }
    
    function getPayment(){
        /** array : insert value session from post **/
        $value  = array('invoiceCash_month_start' => $this->input->post('month'),
                        'invoiceCash_year_start'  => $this->input->post('year'),
                        'invoiceCash_month_end'   => $this->input->post('month2'),
                        'invoiceCash_year_end'    => $this->input->post('year2'),
                        'invoiceCash_customer'    => $this->input->post('customer')
                        );
        $this->session->set_userdata($value); /** set userdata value **/
        redirect('cashflow/cash/payment',301); /** redirect to summary **/
    }
    
    function getPaymentReset(){
        /** array : insert value session from post **/
        $value  = array('invoiceCash_month_start' => 'invoiceCash_month_start',
                        'invoiceCash_year_start'  => 'invoiceCash_year_start',
                        'invoiceCash_month_end'   => 'invoiceCash_month_end',
                        'invoiceCash_year_end'    => 'invoiceCash_year_end',
                        'invoiceCash_customer'    => 'invoiceCash_customer'
                        );
        $this->session->unset_userdata($value); /** set userdata value **/
        redirect('cashflow/cash/payment',301); /** redirect to summary **/
    }
    
    function getPaymentExport(){  
        /** if export via excel **/    
		if($this->input->post('export')=='excel')
            redirect('cashflow/cash/getPaymentExcel/'); 
        /** if export via pdf **/    
        elseif($this->input->post('export')=='pdf')    
            redirect('cashflow/cash/getPaymentPdf/');
        /** if export via email **/  
        elseif($this->input->post('export')=='email')    
            redirect('cashflow/cash/getPaymentEmail');
		else
			echo 'Error redirecting'; /** if error redirecting the page **/
    }
    
    function getPaymentExcel(){
        /** Session Variabel **/
        $month_start  =   $this->session->userdata('invoiceCash_month_start');
        $year_start   =   $this->session->userdata('invoiceCash_year_start');
        $month_end    =   $this->session->userdata('invoiceCash_month_end');
        $year_end     =   $this->session->userdata('invoiceCash_year_end');
        $customer     =   $this->session->userdata('invoiceCash_customer');
        $excel        =   $this->invoices->getExcelInvoicePayment($month_start,$year_start,$month_end,$year_end,$customer);
        $value        =   file_get_contents($excel); /** Read the file's contents **/
        $name         =   'Invoice-Payment.xls'; /** file name content **/
        force_download($name,$value); /** download the file **/
    }
    
    function getPaymentPdf(){
        /** Session Variabel **/
        $month_start  =   $this->session->userdata('invoiceCash_month_start');
        $year_start   =   $this->session->userdata('invoiceCash_year_start');
        $month_end    =   $this->session->userdata('invoiceCash_month_end');
        $year_end     =   $this->session->userdata('invoiceCash_year_end');
        $customer     =   $this->session->userdata('invoiceCash_customer');
        /** go pdf **/
       	$pdf          =   $this->invoices->getPdfInvoicePayment($month_start,$year_start,$month_end,$year_end,$customer);
        $value        =   file_get_contents($pdf); /** Read the file's contents **/
        $name         =   'Invoice-Payment.pdf'; /** file name content **/
        force_download($name,$value); /** download the file **/
    }
    
    function getPaymentEmail(){
       /** Session Variabel **/
        $month_start  =   $this->session->userdata('invoiceCash_month_start');
        $year_start   =   $this->session->userdata('invoiceCash_year_start');
        $month_end    =   $this->session->userdata('invoiceCash_month_end');
        $year_end     =   $this->session->userdata('invoiceCash_year_end');
        $customer     =   $this->session->userdata('invoiceCash_customer');
        /** file excell **/
        $excel        =   $this->invoices->getExcelInvoicePayment($month_start,$year_start,$month_end,$year_end,$customer);
        /** email configuration **/
        $mail_config    =   $this->config->item('mail');
        $smtp_host      =   $mail_config['smtp_host'];
        $smtp_port      =   $mail_config['smtp_port'];
        $from_mail      =   $mail_config['from_mail'];
        $from_name      =   $mail_config['from_name'];
        
        /** php ini set email **/
        ini_set("SMTP",$smtp_host);
        ini_set("smtp_port",$smtp_port);
        ini_set('sendmail_from',$from_mail);
        /** Properties of export **/
        $subject        = 'BBG  SAF Invoice Payment '.month_name($month_start).' '.$year_start.' Sampai '. month_name($month_end).' '.$year_end;
        $contents       = 'Dilampirkan Invoice Payment '.month_name($month_start).' '.$year_start.' Sampai '. month_name($month_end).' '.$year_end;
        $this->email->initialize($mail_config);
        $this->email->from($from_mail,$from_name);
        $email  =   'hendarsyahss@gmail.com';
        $this->email->to($email); /** email to */
        $this->email->subject($subject); /** email subject */
        $this->email->message($contents); /** email content */
        $this->email->attach($excel);  /** email attachement */
        $d=$this->email->send(); /** email send */
        if(!$d):
            $this->session->set_flashdata('error','error');
        else:
			$this->session->set_flashdata('success','success');
        endif;
        redirect('cashflow/cash/payment',301);
    }
    
    function pettyCash(){
        $this->ws->set('title',lang('petty_cash'));/** title page Invoice Payment */
		$this->ws->set('username',$this->session->userdata('username')); /** username session */
		$this->ws->set('department',$this->session->userdata('dep')); /** departement session */
        /** if session already or no to view data **/
        if($this->session->userdata('petty_coa')):
            $no  =   $this->session->userdata('petty_no');
            $coa =   $this->session->userdata('petty_coa');
            $val =   $this->cashs->getRekapPettyCash($no,$coa);
            $per =   indonesian_longDate($val['Tanggal']);
        else:
            $no  =   '-';
            $coa =   '-';
            $val =   '-';
            $per =   '-';
        endif;
        
        $this->ws->set('no',$no); /** variabel no from session **/
        $this->ws->set('coa',$coa); /** variabel coa from session **/
        $this->ws->set('periode',$per); /** variabel Periode **/
        $this->ws->set('cashs',$this->cashs->getCashData()); /** Get Data Table Cash of Coa */
        $this->ws->set('saldo',$this->cashs->getCashSaldo($coa)); /** get saldo cash **/
        $this->ws->set('petty_cash',$this->cashs->getPettyCash($no,$coa));
        $this->ws->set_theme('smooth'); /** set theme to smooth **/
		$this->ws->view('cash/index_pettycash'); /** view layout to views/cash/index_pettycash **/
    }
    
    function getPettyCash(){
        /** array : insert value session from post **/
        $value  = array('petty_no' => $this->input->post('no'),
                        'petty_coa'=> $this->input->post('cash'));
        $this->session->set_userdata($value); /** set userdata value **/
        redirect('cashflow/cash/pettyCash',301); /** redirect to summary **/
    }
    
    function getPettyCashExport(){  
        /** if export via excel **/    
		if($this->input->post('export')=='excel')
            redirect('cashflow/cash/getPettyCashExcel/'); 
        /** if export via pdf **/    
        elseif($this->input->post('export')=='pdf')    
            redirect('cashflow/cash/getPettyCashPdf/');
        /** if export via email **/  
        elseif($this->input->post('export')=='email')    
            redirect('cashflow/cash/getPettyCashEmail');
		else
			echo 'Error redirecting'; /** if error redirecting the page **/
    }
    
    function getPettyCashExcel(){
        /** Session Variabel **/
        $no  =   $this->session->userdata('petty_no');
        $coa =   $this->session->userdata('petty_coa');
        $excel   =   $this->cashs->getExcelPettyCash($no,$coa); /** Excell Pettty Cash**/
        $value   =   file_get_contents($excel); /** Read the file's contents **/
        $name    =   'Petty-Cash.xls'; /** file name content **/
        force_download($name,$value); /** download the file **/
    }
    
    function getPettyCashEmail(){
       /** Session Variabel **/
        $no  =   $this->session->userdata('petty_no');
        $coa =   $this->session->userdata('petty_coa');
        /** file excell **/
        $excel   =   $this->cashs->getExcelPettyCash($no,$coa); /** Excell Pettty Cash**/
        /** email configuration **/
        $mail_config    =   $this->config->item('mail');
        $smtp_host      =   $mail_config['smtp_host'];
        $smtp_port      =   $mail_config['smtp_port'];
        $from_mail      =   $mail_config['from_mail'];
        $from_name      =   $mail_config['from_name'];
        
        /** php ini set email **/
        ini_set("SMTP",$smtp_host);
        ini_set("smtp_port",$smtp_port);
        ini_set('sendmail_from',$from_mail);
        /** Properties of export **/
        $subject        = 'BBG SAF Petty Cash No'.$no.' COA '. $coa; /** subject **/
        $contents       = 'Dilampirkan Petty Cash No'.$no.' COA '. $coa; /** content **/
        $this->email->initialize($mail_config);
        $this->email->from($from_mail,$from_name);
        $email  =   'hendarsyahss@gmail.com';
        $this->email->to($email); /** email to */
        $this->email->subject($subject); /** email subject */
        $this->email->message($contents); /** email content */
        $this->email->attach($excel);  /** email attachement */
        $d=$this->email->send(); /** email send */
        if(!$d):
            $this->session->set_flashdata('error','error');
        else:
			$this->session->set_flashdata('success','success');
        endif;
        redirect('cashflow/cash/pettyCash',301);
    }
    
    function getPettyCashPdf(){
        /** Session Variabel **/
        $no  =   $this->session->userdata('petty_no');
        $coa =   $this->session->userdata('petty_coa');
        /** go pdf **/
       	$pdf          =   $this->cashs->getPdfPettyCash($no,$coa); /** get Petty Cash **/
        $value        =   file_get_contents($pdf); /** Read the file's contents **/
        $name         =   'PettyCash.pdf'; /** file name content **/
        force_download($name,$value); /** download the file **/
    }
    
    function getPettyCashReset(){
        /** array : insert value session from post **/
        $value  = array('petty_no' => 'petty_no',
                        'petty_coa'=> 'petty_coa');
        $this->session->unset_userdata($value); /** set userdata value **/
        redirect('cashflow/cash/pettyCash',301); /** redirect to summary **/
    }
    
    function mutation(){
        $this->ws->set('title',lang('mutation_cash'));/** title page mutation cash */
		$this->ws->set('username',$this->session->userdata('username')); /** username session */
		$this->ws->set('department',$this->session->userdata('dep')); /** departement session */
        /** if session already or no to view data **/
        if($this->session->userdata('mutation_coa')):
            $date_start  =   $this->session->userdata('mutation_dateStart');
            $date_end    =   $this->session->userdata('mutation_dateEnd');
            $coa         =   $this->session->userdata('mutation_coa');
        else:
            $date_start  =   '-';
            $date_end    =   '-';
            $coa         =   '-';
        endif;
        $this->ws->set('date_start',$date_start); /** variabel date start from session **/
        $this->ws->set('date_end',$date_end); /** variabel date end from session **/
        $this->ws->set('coa',$coa); /** variabel customer from session **/
        $this->ws->set('cashs',$this->cashs->getCashData()); /** Get Data Table Customers */
        $this->ws->set('saldo',$this->cashs->getCashSaldoYesterday($coa,$date_start));
        $this->ws->set('mutations',$this->cashs->getMutationCash($date_start,$date_end,$coa));
        $this->ws->set_theme('smooth'); /** set theme to smooth **/
		$this->ws->view('cash/index_mutation'); /** view layout to views/mutation/index_mutation **/
    }
    
    function getMutationCash(){
        /** array : insert value session from post **/
        $value  = array('mutation_dateStart' => $this->input->post('year').'-'.$this->input->post('month').'-'.$this->input->post('day'),
                        'mutation_dateEnd'   => $this->input->post('year2').'-'.$this->input->post('month2').'-'.$this->input->post('day2'),
                        'mutation_coa'       => $this->input->post('cash'));
        $this->session->set_userdata($value); /** set userdata value **/
        redirect('cashflow/cash/mutation',301); /** redirect to mutation **/
    }
    
    function getMutationCashExport(){  
        /** if export via excel **/    
		if($this->input->post('export')=='excel')
            redirect('cashflow/cash/getMutationCashExcel/'); 
        /** if export via pdf **/    
        elseif($this->input->post('export')=='pdf')    
            redirect('cashflow/cash/getMutationCashPdf/');
        /** if export via email **/  
        elseif($this->input->post('export')=='email')    
            redirect('cashflow/cash/getMutationCashEmail');
		else
			echo 'Error redirecting'; /** if error redirecting the page **/
    }
    
    function getMutationCashPdf(){
        /** Session Variabel **/
        $date_start  =   $this->session->userdata('mutation_dateStart');
        $date_end    =   $this->session->userdata('mutation_dateEnd');
        $coa         =   $this->session->userdata('mutation_coa');
        /** go pdf **/
       	$pdf          =   $this->cashs->getPdfMutationCash($date_start,$date_end,$coa); /** get Petty Cash **/
        $value        =   file_get_contents($pdf); /** Read the file's contents **/
        $name         =   'MutationCash.pdf'; /** file name content **/
        force_download($name,$value); /** download the file **/
    }
    
    function getMutationCashExcel(){
        /** Session Variabel **/
        $date_start  =   $this->session->userdata('mutation_dateStart');
        $date_end    =   $this->session->userdata('mutation_dateEnd');
        $coa         =   $this->session->userdata('mutation_coa');
        $excel   =   $this->cashs->getExcelMutationCash($date_start,$date_end,$coa); /** get Mutation Cash Excel **/
        $value   =   file_get_contents($excel); /** Read the file's contents **/
        $name    =   'Mutation-Cash.xls'; /** file name content **/
        force_download($name,$value); /** download the file **/
    }
    
    function getMutationCashEmail(){
       /** Session Variabel **/
        $date_start  =   $this->session->userdata('mutation_dateStart');
        $date_end    =   $this->session->userdata('mutation_dateEnd');
        $coa         =   $this->session->userdata('mutation_coa');
        /** file excell **/
        $excel   =   $this->cashs->getExcelMutationCash($date_start,$date_end,$coa); /** Excell Pettty Cash**/
        /** email configuration **/
        $mail_config    =   $this->config->item('mail');
        $smtp_host      =   $mail_config['smtp_host'];
        $smtp_port      =   $mail_config['smtp_port'];
        $from_mail      =   $mail_config['from_mail'];
        $from_name      =   $mail_config['from_name'];
        
        /** php ini set email **/
        ini_set("SMTP",$smtp_host);
        ini_set("smtp_port",$smtp_port);
        ini_set('sendmail_from',$from_mail);
        /** Properties of export **/
        $subject        = 'BBG SAF Mutation COA '. $coa .' Tanggal '.indonesian_date($date_start).' s/d '.indonesian_date($date_end); /** subject **/
        $contents       = 'Dilampirkan Mutation COA '. $coa .' Tanggal '.indonesian_date($date_start).' s/d '.indonesian_date($date_end); /** content **/
        $this->email->initialize($mail_config);
        $this->email->from($from_mail,$from_name);
        $email  =   'hendarsyahss@gmail.com';
        $this->email->to($email); /** email to */
        $this->email->subject($subject); /** email subject */
        $this->email->message($contents); /** email content */
        $this->email->attach($excel);  /** email attachement */
        $d=$this->email->send(); /** email send */
        if(!$d):
            $this->session->set_flashdata('error','error');
        else:
			$this->session->set_flashdata('success','success');
        endif;
        redirect('cashflow/cash/mutation',301);
    }
    
    function getMutationCashReset(){
        /** array : delete value session from post **/
        $value  = array('mutation_dateStart' => 'mutation_dateStart',
                        'mutation_dateEnd'   => 'mutation_dateEnd',
                        'mutation_coa'       => 'mutation_coa');
        $this->session->unset_userdata($value); /** unset userdata value **/
        redirect('cashflow/cash/mutation',301); /** redirect to mutation **/
    }
    
    function generalLedger(){
        $this->ws->set('title',lang('general_ledger'));/** title page General Ledger */
		$this->ws->set('username',$this->session->userdata('username')); /** username session */
		$this->ws->set('department',$this->session->userdata('dep')); /** departement session */
        /** if tax session already or no to view data **/
        if($this->session->userdata('ledger_month')):
            $month  =   $this->session->userdata('ledger_month');
            $year   =   $this->session->userdata('ledger_year');
            $account=   $this->session->userdata('ledger_acc');
        else:
            $month  =   '-';
            $year   =   '-';
            $account=   '-';
        endif;
        $this->ws->set('month',$month); /** variabel date month from session **/
        $this->ws->set('year',$year); /** variabel year from session **/
        $this->ws->set('account',$account); /** variabel Account from session **/   
        $this->ws->set('budgets',$this->budgets->getDataBudget('H1','H2')); /** Get Data Table Rekening */
        $this->ws->set('accounts',$this->budgets->getBudgetAccount(substr($account,0,3))); /** get ledger in cash **/
        $this->ws->set_theme('smooth'); /** set theme to smooth **/
		$this->ws->view('cash/index_ledger'); /** view layout to views/cash/index_ledger **/
    } 
    
    function getGeneralLedger(){
        /** array : insert value session from post **/
        $value  = array('ledger_month' => $this->input->post('month'),
                        'ledger_year'  => $this->input->post('year'),
                        'ledger_acc'   => $this->input->post('budget'));
        $this->session->set_userdata($value); /** set userdata value **/
        redirect('cashflow/cash/generalLedger',301); /** redirect to generalLedger **/
    }
    
    function getGeneralLedgerExport(){  
        /** if export via excel **/    
		if($this->input->post('export')=='excel')
            redirect('cashflow/cash/getGeneralLedgerExcel/'); 
        /** if export via pdf **/    
        elseif($this->input->post('export')=='pdf')    
            redirect('cashflow/cash/getGeneralLedgerPdf/');
        /** if export via email **/  
        elseif($this->input->post('export')=='email')    
            redirect('cashflow/cash/getGeneralLedgerEmail');
		else
			echo 'Error redirecting'; /** if error redirecting the page **/
    }
    
    function getGeneralLedgerPdf(){
        /** Session Variabel **/
        $month  =   $this->session->userdata('ledger_month');
        $year   =   $this->session->userdata('ledger_year');
        $account=   $this->session->userdata('ledger_acc');
        /** go pdf **/
       	$pdf          =   $this->cashs->getPdfGeneralLedger($month,$year,$account); /** get Pdf GL **/
        $value        =   file_get_contents($pdf); /** Read the file's contents **/
        $name         =   'MutationCash.pdf'; /** file name content **/
        force_download($name,$value); /** download the file **/
    }
    
    function getGeneralLedgerReset(){
        /** array : insert value session from post **/
        $value  = array('ledger_month' => 'ledger_month',
                        'ledger_year'  => 'ledger_year',
                        'ledger_acc'   => 'ledger_acc');
        $this->session->unset_userdata($value); /** unset userdata value **/
        redirect('cashflow/cash/generalLedger',301); /** redirect to generalLedger **/
    }
    
    
 }   